package com.iwomy.envpro.platform.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.envpro.common.core.util.R;
import com.iwomy.envpro.common.log.annotation.SysLog;
import com.iwomy.envpro.platform.entity.AirMonitorEquipmentEntity;
import com.iwomy.envpro.platform.entity.BasicTriggerEntity;
import com.iwomy.envpro.platform.entity.DataTemplateEntity;
import com.iwomy.envpro.platform.exception.PlatformException;
import com.iwomy.envpro.platform.service.AirMonitorEquipmentService;
import com.iwomy.envpro.platform.service.DataTemplateService;
import com.iwomy.envpro.platform.utils.DeptFillUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.envpro.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * 数据模板
 *
 * @author pig
 * @date 2023-09-26 14:55:07
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/dataTemplate")
@Tag(description = "dataTemplate", name = "数据模板管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class DataTemplateController {

	private final DataTemplateService dataTemplateService;

	private final AirMonitorEquipmentService airMonitorEquipmentService;

	/**
	 * 分页查询
	 *
	 * @param page         分页对象
	 * @param dataTemplate 数据模板
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_dataTemplate_view')")
	public R getDataTemplatePage(@ParameterObject Page page, @ParameterObject DataTemplateEntity dataTemplate) {
		page = dataTemplateService.pageByScope(page, dataTemplate);
		DeptFillUtils.fill(page);
		return R.ok(page);
	}


	/**
	 * 通过id查询数据模板
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_dataTemplate_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(dataTemplateService.getByIdDeep(id));
	}

	/**
	 * 新增数据模板
	 *
	 * @param dataTemplate 数据模板
	 * @return R
	 */
	@Operation(summary = "新增数据模板", description = "新增数据模板")
	@SysLog("新增数据模板")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_dataTemplate_add')")
	public R saveDeep(@RequestBody DataTemplateEntity dataTemplate) {

		DataTemplateEntity wrapperEntity = new DataTemplateEntity();
		wrapperEntity.setTemplateName(dataTemplate.getTemplateName());
		LambdaQueryWrapper<DataTemplateEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		DataTemplateEntity one = dataTemplateService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one)) {
			return R.failed("数据模板已存在");
		}
		return R.ok(dataTemplateService.saveDeep(dataTemplate));
	}

	/**
	 * 修改数据模板
	 *
	 * @param dataTemplate 数据模板
	 * @return R
	 */
	@Operation(summary = "修改数据模板", description = "修改数据模板")
	@SysLog("修改数据模板")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_dataTemplate_edit')")
	public R updateDeep(@RequestBody DataTemplateEntity dataTemplate) {
		LambdaQueryWrapper<AirMonitorEquipmentEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(AirMonitorEquipmentEntity::getDataTemplateId, dataTemplate.getId());
		long count = airMonitorEquipmentService.count(queryWrapper);
		if (count > 0) {
			return R.failed("数据模板关联了设备，不允许编辑");
		}


		DataTemplateEntity wrapperEntity = new DataTemplateEntity();
		wrapperEntity.setTemplateName(dataTemplate.getTemplateName());
		LambdaQueryWrapper<DataTemplateEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		DataTemplateEntity one = dataTemplateService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one) && !one.getId().equals(dataTemplate.getId())) {
			throw new PlatformException("数据模板已存在");
		}

		return R.ok(dataTemplateService.updateDeep(dataTemplate));
	}

	/**
	 * 通过id删除数据模板
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除数据模板", description = "通过id删除数据模板")
	@SysLog("通过id删除数据模板")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_dataTemplate_del')")
	public R removeDeep(@RequestBody Long[] ids) {

		LambdaQueryWrapper<AirMonitorEquipmentEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.in(AirMonitorEquipmentEntity::getDataTemplateId, ids);
		long count = airMonitorEquipmentService.count(queryWrapper);
		if (count > 0) {
			return R.failed("数据模板关联了设备，不允许删除");
		}


		return R.ok(dataTemplateService.removeDeep(ids));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param dataTemplate 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_dataTemplate_export')")
	public List<DataTemplateEntity> export(DataTemplateEntity dataTemplate) {
		return dataTemplateService.listDeep(Wrappers.query(dataTemplate));
	}

	/**
	 * @param dataTemplate 查询条件
	 */
	@GetMapping("/list")
	public R list(DataTemplateEntity dataTemplate) {
		return R.ok(dataTemplateService.listByScope(dataTemplate));
	}


	/**
	 * 根据所选部门 查看有权限的部门数据
	 *
	 * @param deptId
	 */
	@GetMapping("/listByDeptId/{deptId}")
	public R listByDeptId(@PathVariable("deptId") Long deptId) {
		return R.ok(dataTemplateService.listByDeptId(deptId));
	}


	/**
	 * @param dataTemplate 查询条件
	 */
	@GetMapping("/factorDetailList")
	public R factorDetailList(DataTemplateEntity dataTemplate) {
		return R.ok(dataTemplateService.factorDetailList(dataTemplate));
	}


}